/**
    算法描述：
    核心问题：
    解决所需数据结构+算法：
**/
#include<bits/stdc++.h>
using namespace std;
 
struct edge{
    int x, y, c;
}a[500050];
 
int n, m, ans = 0, f[1010];
 
bool cmp(edge x, edge y) {
    return x.c < y.c;
}
 
int findfa(int x) {
    return f[x] = f[x] == x ? x : findfa(f[x]);
}
 
int main() {
    int count = 0;
    scanf("%d%d", &n, &m);
    for(int i=1; i<=n; i++)
        f[i] = i;
    for(int i=1; i<=m; i++) {
        int x, y, c;
        scanf("%d%d%d", &x, &y, &c);
        a[i] = edge{x, y, c};
    }
    sort(a+1, a+1+m, cmp);
    for(int i=1; i<=m; i++) {
        int x = findfa(a[i].x);
        int y = findfa(a[i].y);
        if (x != y) {
            f[x] = y;
            ans += a[i].c;
            count++;
            if(count == n-1)
                break;
        }
    }
    printf("%d\n", ans);
     
    return 0;
}